package com.sfac.springBoot.module.test.service.Impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.sfac.springBoot.module.common.vo.Result;
import com.sfac.springBoot.module.common.vo.Search;
import com.sfac.springBoot.module.test.dao.CityDao;
import com.sfac.springBoot.module.test.dao.CityDao2;
import com.sfac.springBoot.module.test.pojo.City;
import com.sfac.springBoot.module.test.service.CityService;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.persistence.criteria.CriteriaBuilder;
import java.util.*;

/**
 * personal demo
 **/

@Service
public class CityServiceImpl implements CityService {

    @Autowired
    private CityDao cityDao;

    @Autowired
    private CityDao2 cityDao2;

    @Override
    @Transactional
    public Result<City> insertCity(City city) {
        City temp = cityDao.getCityByCityName(city.getCityName());
        if(temp!=null){
            return new Result<>(Result.ResultStatus.FAILED.code,"city name is repeated");
        }

        city.setDateCreated(new Date());
        city.setDateModified(new Date());

//        cityDao.insertCity(city);
        cityDao2.insert(city);

        return new Result<>(Result.ResultStatus.SUCCESS.code,"insert successfully",city);
    }

    @Override
    @Transactional
    public Result<City> updateCity(City city) {
        City temp = cityDao.getCityByCityId(city.getCityId());
        if(temp==null){
            return new Result<>(Result.ResultStatus.FAILED.code,"city does not exist!");
        }
        city.setDateModified(city.getDateModified());
//        cityDao.updateCity(city);
        UpdateWrapper<City> wrapper = new UpdateWrapper<>();
        wrapper.set(StringUtils.isNotBlank(city.getCityName()),
                "city_name",city.getCityName());
        wrapper.set(StringUtils.isNotBlank(city.getLocalCityName()),
                "local_city_name",city.getLocalCityName());
        wrapper.eq(true,"city_id",city.getCityId());
        cityDao2.update(city,wrapper);
        return new Result<>(Result.ResultStatus.SUCCESS.code,"update successfully",city);
    }

    @Override
    @Transactional
    public Result<Object> deleteCity(int Id) {
        City temp = cityDao.getCityByCityId(Id);
        if(temp==null){
            return new Result<>(Result.ResultStatus.FAILED.code,"city does not exist!");
        }
//        cityDao.deleteCity(Id);
        cityDao2.deleteById(Id);
        return new Result<>(Result.ResultStatus.SUCCESS.code,"delete successfully");
    }

    @Override
    @Transactional
    public Result<City> selectCityById(int id) {
        City city = cityDao.getCityByCityId(id);
        if(city==null){
            return new Result<>(Result.ResultStatus.FAILED.code,"city does not exist!");
        }
        return new Result<>(Result.ResultStatus.SUCCESS.code,"select correct",city);
    }

    @Override
    @Transactional
    public PageInfo<City> getCitiesBySearch(Search search) {
        search.init();
        PageHelper.startPage(search.getCurrentPage(),search.getPageSize());
        return PageInfo.of(Optional.ofNullable
                (cityDao.getCitiesBySearch(search)).orElse(Collections.emptyList()));
    }


    @Override
    @Transactional
    public Page<City> getCitiesBySearch2(Search search){
        search.init();
        Page<City> page = new Page<>(search.getCurrentPage(), search.getPageSize());
        QueryWrapper<City> wrapper = new QueryWrapper<>();
        wrapper.like(
                search.getKeyword() != null && !search.getKeyword().equals(""),
                "city_name",
                search.getKeyword()
        ).or().like(
                search.getKeyword() != null && !search.getKeyword().equals(""),
                "local_city_name",
                search.getKeyword()
        ).orderBy(
                true,
                StringUtils.isBlank(search.getDirection())||
                        search.getDirection().toLowerCase().equals("asc"),
                StringUtils.isBlank(search.getSort())?"city_id":search.getSort()
        );
        return cityDao2.selectPage(page,wrapper);

    }

    @Override
    public List<City> getCitiesByCountryId(Integer id) {
        return  cityDao.getCitiesByCountryId(id);
    }
}
